package com.bluemson.service.Impl;

import com.bluemson.dao.GetDao;
import com.bluemson.dao.ManagerDao;
import com.bluemson.dao.SearchDao;
import com.bluemson.entity.Article;
import com.bluemson.entity.Comment;
import com.bluemson.entity.Plate;
import com.bluemson.entity.User;
import com.bluemson.service.GetService;
import com.bluemson.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class GetServiceImpl implements GetService {
    private final SqlSession session = MyBatisUtil.getSqlSession();
    GetDao dao = session.getMapper(GetDao.class);
    @Override
    public List<Plate> getPlate(){
        List<Plate> plates = dao.getPlate();
        for(Plate plate:plates){
            String name = plate.getName();
            plate.setHosts(dao.getHosts(name));
        }
        return plates;
    }
    @Override
    public List<String> getHost(String name){
        return dao.getHosts(name);
    }
    @Override
    public int[] getHostId(String name){
        List<String> hosts = dao.getHosts(name);
        int[] ids = new int[100];
        int i=0;
        for (String username:hosts){
            ids[i]=dao.getId(username);
            i++;
        }
        int[] hostIds = new int[i];
        System.arraycopy(ids, 0, hostIds, 0, i);
        return hostIds;
    }
    @Override
    public List<Plate> getHostPlate(String username){
        return dao.getHostPlate(username);
    }
    @Override
    public List<String> getConcernedPlate(String username){
        return dao.getConcernedPlate(username);
    }
    @Override
    public List<Article> getArticle(){
        return dao.getArticle();
    }
    @Override
    public List<Article> getUserArticle(String username){
        return dao.getUserArticle(username);
    }
    @Override
    public Article getIdArticle(int id){
        Article article = dao.getIdArticle(id);
        article.setUserHead(dao.getHead(article.getUsername()));
        return article;
    }
    @Override
    public List<Article> getPlateArticle(String plate){
        return dao.getPlateArticle(plate);
    }
    @Override
    public List<Article> getPlateHot(String plate){
        return dao.getPlateHot(plate);
    }
    @Override
    public List<User> getAllUser(){
        return dao.getAllUser();
    }
    @Override
    public User getInformation(int id){
        return dao.getInformation(id);
    }
    @Override
    public List<Comment> getComment(int id){
        List<Comment> comments = dao.getComment(id);
        for(Comment comment:comments){
            comment.setReplyList(dao.getReply(comment.getId()));
            comment.setUserHead(dao.getHead(comment.getUsername()));
        }
        return comments;
    }
    @Override
    public List<Comment> getReply(int id){
        return dao.getReply(id);
    }
    @Override
    public List<User> searchUser(String username){
        ManagerDao managerDao = session.getMapper(ManagerDao.class);
        return managerDao.searchUser(username);
    }
    @Override
    public List<Article> searchArticle(String article){
        SearchDao searchDao = session.getMapper(SearchDao.class);
        return searchDao.article(article);
    }
    @Override
    public Plate searchPlate(String name){
        SearchDao searchDao = session.getMapper(SearchDao.class);
        return searchDao.plate(name);
    }

    @Override
    public List<Article> getHotArticle() {
        return dao.getHotArticle();
    }
}
